package com.xauto.pioneer.appuser.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.model.Bank;
import com.xauto.pioneer.appuser.model.Payment;

/**
 * Created by Lemon on 2015/12/11.
 * Desc:支付方式 适配器
 */
public class BankAdapter extends AbsAdapter<Bank> {

    public BankAdapter(Activity context, int layout) {
        super(context, layout);
    }
    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = getDataList().get(i).getBank_prefix();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }
    public int getSectionForPosition(int position) {
        return getDataList().get(position).getBank_prefix().charAt(0);
    }
    @Override
    public ViewHolder<Bank> getHolder() {
        return new TemplateViewHolder();
    }


    private class TemplateViewHolder implements ViewHolder<Bank> {
        private TextView letter_text;
        private TextView bank_name_text;

        @Override
        public void initViews(View v, int position) {
            letter_text = (TextView) v.findViewById(R.id.letter_text);
            bank_name_text = (TextView) v.findViewById(R.id.bank_name_text);
        }
        @Override
        public void updateData(Bank bank, int position) {
            bank_name_text.setText(bank.getBank_name());
            //根据position获取分类的首字母的Char ascii值
            int section = getSectionForPosition(position);
            //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            if(position == getPositionForSection(section)){
                letter_text.setVisibility(View.VISIBLE);
               letter_text.setText(bank.getBank_prefix());
            }else{
                letter_text.setVisibility(View.GONE);
            }
        }

        @Override
        public void doOthers(Bank bank, int position) {

        }
    }
}
